package com.ruoyi.teacher.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.teacher.domain.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teacher.mapper.TeacherMapper;
import com.ruoyi.teacher.domain.Teacher;
import com.ruoyi.teacher.service.ITeacherService;

/**
 * 老师Service业务层处理
 *
 * @author TengZeng
 * @date 2023-05-18
 */
@Service
public class TeacherServiceImpl implements ITeacherService
{
    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 查询老师
     *
     * @param id 老师主键
     * @return 老师
     */
    @Override
    public Teacher selectTeacherById(Long id)
    {
        return teacherMapper.selectTeacherById(id);
    }

    /**
     * 查询老师列表
     *
     * @param teacher 老师
     * @return 老师
     */
    @Override
    public List<Teacher> selectTeacherList(Teacher teacher)
    {
        return teacherMapper.selectTeacherList(teacher);
    }

    /**
     * 新增老师
     *
     * @param teacher 老师
     * @return 结果
     */
    @Override
    public int insertTeacher(Teacher teacher)
    {
        return teacherMapper.insertTeacher(teacher);
    }

    /**
     * 修改老师
     *
     * @param teacher 老师
     * @return 结果
     */
    @Override
    public int updateTeacher(Teacher teacher)
    {
        return teacherMapper.updateTeacher(teacher);
    }

    /**
     * 批量删除老师
     *
     * @param ids 需要删除的老师主键
     * @return 结果
     */
    @Override
    public int deleteTeacherByIds(Long[] ids)
    {
        return teacherMapper.deleteTeacherByIds(ids);
    }

    /**
     * 删除老师信息
     *
     * @param id 老师主键
     * @return 结果
     */
    @Override
    public int deleteTeacherById(Long id)
    {
        return teacherMapper.deleteTeacherById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<Teacher> findAll() {
        return teacherMapper.findAll();
    }

    /**
     * 老师无限级树
     * @return
     */
    @Override
    public List<Teacher> teacherTreeSelect() {
        List<Teacher> result = new ArrayList<>();
        // 查询出来所有
        List<Teacher> allTeacher = teacherMapper.findAll();
        // 转成map key id value teacher
        Map<Long, Teacher> teacherMap = allTeacher.stream().collect(Collectors.toMap(Teacher::getId, m -> m));
        // 遍历集合
        allTeacher.forEach(mu -> {
            if (null == mu.getParentId()) {
                result.add(mu);
            } else {
                Teacher parent = teacherMap.get(mu.getParentId());
                parent.getChildren().add(mu);
            }
        });
        return result;
    }
}
